package com.qianxx.base.utils;

import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.provider.CalendarContract.CalendarAlerts;
import android.util.Log;

/**
 * 用于显示订单出发时间
 * */
public class TimeUtil {

	/**
	 * 将时间戳转换成日期
	 * */
	public static String format(long time) {
		String[] str = getJudge(); // str[0]为今天，str[1]为昨天

		return format(time, str);
	}

	public static String format(long time, String[] str) {
		StringBuilder display = new StringBuilder();

		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(time);
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		String date = getDate(year, month, day);
		if (date.equals(str[0])) {
			display.append("今天");
		} else if (date.equals(str[1])) {
			display.append("昨天");
		} else {
			display.append(getDate(month, day));
		}
		display.append(" "); // 加空格
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		int min = cal.get(Calendar.MINUTE);
		display.append(getTime(hour, min));

		return display.toString();
	}

	/**
	 * 获得今天/昨天的日期
	 * */
	public static String[] getJudge() {
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		String today = getDate(year, month, day);

		cal.add(Calendar.DAY_OF_MONTH, -1); // 减一天
		year = cal.get(Calendar.YEAR);
		month = cal.get(Calendar.MONTH) + 1;
		day = cal.get(Calendar.DAY_OF_MONTH);
		String yesterday = getDate(year, month, day);

		LogUtil.v("今天:" + today + " | 昨天:" + yesterday);
		return new String[] { today, yesterday };
	}

	public static String getDate(int year, int month, int day) {
		StringBuilder str = new StringBuilder();
		str.append(year);
		str.append("-");
		str.append(month);
		str.append("-");
		str.append(day);
		return str.toString();
	}

	public static String getDate(int month, int day) {
		StringBuilder str = new StringBuilder();
		if (month < 10) {
			str.append("0");
		}
		str.append(month);
		str.append("-");
		if (day < 10) {
			str.append("0");
		}
		str.append(day);

		return str.toString();
	}

	public static String getTime(int hour, int min) {
		StringBuilder str = new StringBuilder();
		if (hour < 10) {
			str.append("0");
		}
		str.append(hour);
		str.append(":");
		if (min < 10) {
			str.append("0");
		}
		str.append(min);

		return str.toString();
	}

	/**
	 * 获取日期
	 * */
	public static String getDate(long time) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(time);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		return getDate2(month, day);
	}

	public static String getDate2(int month, int day) {
		StringBuilder str = new StringBuilder();
		str.append(month);
		str.append("月");
		str.append(day);
		str.append("日");
		return str.toString();
	}

	/**
	 * 获取日期
	 * */
	public static String formatDate(long time) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(time);
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		return getDate3(year, month, day);
	}

	private static String getDate3(int year, int month, int day) {
		StringBuilder str = new StringBuilder();
		str.append(year);
		str.append("-");
		if (month < 10) {
			str.append("0");
		}
		str.append(month);
		str.append("-");
		if (day < 10) {
			str.append("0");
		}
		str.append(day);
		return str.toString();
	}

	/**
	 * 0表示今天，1表示明天，2表示后天
	 */
	public static int getDateType(long time) {
		Calendar cal = Calendar.getInstance();
		long currentTime = cal.getTimeInMillis();
		if (time <= currentTime) {
			return 0;
		}
		cal.set(Calendar.MILLISECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.HOUR_OF_DAY, 24);
		long time0 = cal.getTimeInMillis(); // 今天24点的时间
		if (time < time0) {
			return 0;
		}
		cal.add(Calendar.DAY_OF_MONTH, 1);
		long time1 = cal.getTimeInMillis(); // 明天24点的时间
		if (time < time1) {
			return 1;
		} else {
			return 2;
		}
	}
	
	/**
	 * 将时间戳转为字符串
	 */
	public static String getStrTime(long cc_time) {
		String re_StrTime = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yy年MM月dd日 HH:mm");
		// 例如：cc_time=1291778220
		re_StrTime = sdf.format(new Date(cc_time));
		return re_StrTime;
	}
	/**
	 * 将时间戳转为字符串
	 */
	public static String getStrTime2(long cc_time) {
		String re_StrTime = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// 例如：cc_time=1291778220
		re_StrTime = sdf.format(new Date(cc_time));
		return re_StrTime;
	}
	/**
	 * 将时间戳转为字符串
	 */
	public static String getStrTime3(long cc_time) {
		String re_StrTime = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// 例如：cc_time=1291778220
		re_StrTime = sdf.format(new Date(cc_time));
		return re_StrTime;
	}
	/**
	 * 将时间戳转为字符串
	 */
	public static int getStrTimeYear2(long cc_time) {
		String re_StrTime = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		// 例如：cc_time=1291778220
		re_StrTime = sdf.format(new Date(cc_time));
		return Integer.parseInt(re_StrTime);
	}
	/**
	 * 将时间戳转为字符串
	 */
	public static int getStrTimeMonth2(long cc_time) {
		String re_StrTime = null;
		SimpleDateFormat sdf = new SimpleDateFormat("MM");
		// 例如：cc_time=1291778220
		re_StrTime = sdf.format(new Date(cc_time));
		return Integer.parseInt(re_StrTime);
	}
	/**
	 * 将时间戳转为字符串
	 */
	public static int getStrTimeDay2(long cc_time) {
		String re_StrTime = null;
		SimpleDateFormat sdf = new SimpleDateFormat("dd");
		// 例如：cc_time=1291778220
		re_StrTime = sdf.format(new Date(cc_time));
		return Integer.parseInt(re_StrTime);
	}
	/**
	 * 将时间戳转为字符串
	 */
	public static String getStrTime4(long cc_time) {
		String re_StrTime = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		// 例如：cc_time=1291778220
		re_StrTime = sdf.format(new Date(cc_time));
		return re_StrTime;
	}

	/**
	 * 将时间戳转为字符串
	 */
	public static String getStrTime5(long cc_time) {
		String re_StrTime = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
		re_StrTime = sdf.format(new Date(cc_time));
		return re_StrTime;
	}
	/**
	 * 
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static int getTimeSpace(String time1,String time2){
		int d = 0;
		try {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			Long c = sf.parse(time2).getTime() - sf.parse(time1).getTime();
			d = (int) (c/1000/60/60/24);//天
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return d;
	}

	/**
	 * 将字符串转为时间戳
	 */
	public static long getLongTime(String time){
		try {  
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
            return sdf.parse(time).getTime();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return 0;
	}
	/**
	 * 时间拼接到18:00:00
	 * @param time
	 * @return
	 */
	public static String getEndTime(long time){
		String re_StrTime = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// 例如：cc_time=1291778220
		re_StrTime = sdf.format(new Date(time));
//		Log.i("ss", re_StrTime);
		
        return re_StrTime;
	}
	/**
	 * 获取当前时间，是24小时制的long型
	 */
	public static String getCurrentDate() {
		long l = System.currentTimeMillis();
		//new日期对象
		Date date = new Date(l);
		//转换提日期输出格式
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
		String timeStr = dateFormat.format(date);
//		Log.i("ss", timeStr);
		return timeStr;
	}
	/**
	 * 获取当前时间，是24小时制的long型
	 */
	public static String getCurrentTime() {
		long l = System.currentTimeMillis();
		//new日期对象
		Date date = new Date(l);
		//转换提日期输出格式
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String timeStr = dateFormat.format(date);
//		Log.i("ss", timeStr);
		return timeStr;
	}
	/**
	 * 获取当前时间，是24小时制的long型
	 */
	public static String getCurrentTime2() {
		long l = System.currentTimeMillis();
		//new日期对象
		Date date = new Date(l);
		//转换提日期输出格式
		SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
		String timeStr = dateFormat.format(date);
//		Log.i("ss", timeStr);
		return timeStr;
	}
	/**
	 * 两个时间差多少
	 * @param t1
	 * @param t2
	 * @return
	 */
	public static String getTimeSpace2(String t1,String t2){
		String spaceTime = "";
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		try { 
		//前的时间 
		java.util.Date fd = df.parse(t1); 
		//后的时间 
		java.util.Date td = df.parse(t2); 
		//两时间差,精确到毫秒 
		long diff = td.getTime() - fd.getTime(); 
		if (diff <=  0) {
			return "";
		}
		long day = diff / 86400000; //以天数为单位取整 
		long hour= diff % 86400000 / 3600000; //以小时为单位取整 
		long min = diff % 86400000 % 3600000 / 60000; //以分钟为单位取整 
		long seconds = diff % 86400000 % 3600000 % 60000 / 1000; //以秒为单位取整 
		//天时分秒 
//		System.out.println("两时间差---> " +day+"天"+hour+"小时"+min+"分"+seconds+"秒"); 
		if (day == 0) {
			spaceTime = hour+"时"+min+"分"+seconds+"秒";
		}else {
			spaceTime = day+"天 "+hour+"时"+min+"分"+seconds+"秒";
		}
		
		
		} catch (ParseException e) { 
		e.printStackTrace(); 
		}
		return spaceTime;
	}
	/**
	 * 取得当前日期之后N天的日期
	 * @param n
	 * @return
	 */
	public static String afterNDay(int n) {
		long l = System.currentTimeMillis();
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		c.setTime(new Date(l));
		c.add(Calendar.DATE, n);
		java.util.Date d2 = c.getTime();
		String s = df.format(d2);
		return s;
	}
	/**
	 * 取得l日期之后N天的日期
	 * @param l
	 * @param n
	 * @return
	 */
	public static String afterNDay(long l,int n) {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		c.setTime(new Date(l));
		c.add(Calendar.DATE, n);
		java.util.Date d2 = c.getTime();
		String s = df.format(d2);
		return s;
	}


	/**,
	 * 判断是否小于等于当天时间
	 */
	public static boolean validCurrentTime(long time) {
		long l = System.currentTimeMillis();//当前时间
		if (time <= l) {
			return true;
		}
		return false;
	}

}
